package org.hbgl.geoentity.db.service;

import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.hbgl.geoentity.db.dao.GePermissionMapper;
import org.hbgl.geoentity.db.dao.GeRoleMapper;
import org.hbgl.geoentity.db.domain.GePermission;
import org.hbgl.geoentity.db.domain.GePermissionExample;
import org.hbgl.geoentity.db.domain.GeRole;
import org.hbgl.geoentity.db.domain.GeRoleExample;
import org.hbgl.geoentity.db.util.EnumFile;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class GeRoleService {
    @Resource
    private GeRoleMapper roleMapper;
    @Resource
    private GePermissionMapper permissionMapper;

    public Set<String> queryByIds(Integer[] roleIds) {
        Set<String> roles = new HashSet<>();
        if (roleIds.length == 0) {
            return roles;
        }

        GeRoleExample example = new GeRoleExample();
        example.or().andIdIn(Arrays.asList(roleIds)).andStateEqualTo(EnumFile.CommonState.Normal);
        List<GeRole> roleList = roleMapper.selectByExample(example);

        for (GeRole role : roleList) {
            roles.add(role.getRoleName());
        }

        return roles;
    }

    public List<GeRole> allNormal(Integer unitId, String keywords, Integer page, Integer limit, String sort, String order) {
        GeRoleExample example = new GeRoleExample();
        GeRoleExample.Criteria criteria = example.or();

        criteria.andUnitIdEqualTo(unitId);
        if (StringUtils.isNotBlank(keywords)) {
            criteria.andRoleNameLike("%" + keywords + "%");
        }
        criteria.andStateNotEqualTo(EnumFile.CommonState.Delete);

        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return roleMapper.selectByExample(example);
    }

    public GeRole findById(Integer unitId, Integer id) {
        GeRoleExample example = new GeRoleExample();
        example.or().andUnitIdEqualTo(unitId).andIdEqualTo(id).andStateNotEqualTo(EnumFile.CommonState.Delete);
        return roleMapper.selectOneByExample(example);
    }

    public List<GeRole> findByRoleName(Integer unitId, String name) {
        GeRoleExample example = new GeRoleExample();
        example.or().andUnitIdEqualTo(unitId).andRoleNameEqualTo(name).andStateNotEqualTo(EnumFile.CommonState.Delete);
        return roleMapper.selectByExample(example);
    }

    public List<GeRole> findByRoleName(Integer unitId, String name, Integer id) {
        GeRoleExample example = new GeRoleExample();
        example.or().andUnitIdEqualTo(unitId).andRoleNameEqualTo(name).andIdNotEqualTo(id).andStateNotEqualTo(EnumFile.CommonState.Delete);
        return roleMapper.selectByExample(example);
    }

    @Transactional
    public void add(GeRole role, List<String> permissions) {
        role.setState(EnumFile.CommonState.Normal);
        role.setAddTime(LocalDateTime.now());
        role.setEditTime(LocalDateTime.now());
        roleMapper.insertSelective(role);

        // 先删除旧的权限，再更新新的权限
        // GePermissionExample example = new GePermissionExample();
        // example.or().andRoleIdEqualTo(role.getId()).andStateEqualTo(EnumFile.CommonState.Normal);
        // permissionMapper.deleteByExample(example);
        for (String permission : permissions) {
            GePermission gePermission = new GePermission();
            gePermission.setRoleId(role.getId());
            gePermission.setPermission(permission);
            gePermission.setState(EnumFile.CommonState.Normal);
            gePermission.setAddTime(LocalDateTime.now());
            gePermission.setEditTime(LocalDateTime.now());
            permissionMapper.insertSelective(gePermission);
        }
    }

    @Transactional
    public int updateById(GeRole role, List<String> permissions) {
        // 先删除旧的权限，再更新新的权限
        GePermissionExample example = new GePermissionExample();
        example.or().andRoleIdEqualTo(role.getId());
        permissionMapper.deleteByExample(example);
        for (String permission : permissions) {
            GePermission gePermission = new GePermission();
            gePermission.setRoleId(role.getId());
            gePermission.setPermission(permission);
            gePermission.setState(EnumFile.CommonState.Normal);
            gePermission.setAddTime(LocalDateTime.now());
            gePermission.setEditTime(LocalDateTime.now());
            permissionMapper.insertSelective(gePermission);
        }

        role.setEditTime(LocalDateTime.now());
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    @Transactional
    public int deleteByIdLogical(Integer id) {
        GePermission permission = new GePermission();
        permission.setRoleId(id);
        permission.setState(EnumFile.CommonState.Delete);
        permission.setEditTime(LocalDateTime.now());
        GePermissionExample example = new GePermissionExample();
        example.or().andRoleIdEqualTo(id);
        permissionMapper.updateByExampleSelective(permission, example);

        GeRole role = new GeRole();
        role.setId(id);
        role.setState(EnumFile.CommonState.Delete);
        role.setEditTime(LocalDateTime.now());
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    public List<GeRole> allNormal(Integer unitId) {
        GeRoleExample example = new GeRoleExample();
        GeRoleExample.Criteria criteria = example.or();

        criteria.andUnitIdEqualTo(unitId).andStateEqualTo(EnumFile.CommonState.Normal);

        return roleMapper.selectByExample(example);
    }
}
